<!DOCTYPE html>
<html lang="en">
<head>
    {template 'common/listhead'}

    <!--微擎 -->
    <!--这个放在最后一切平白安全-->
    <script type="text/javascript" src="./resource/js/require.js?v=20170915"></script>


</head>
</head>

<body class="nav-md">
<div class="container body">

    <div class="main_container xc_edit_from"  style="overflow-x: hidden;min-height: 100vh;background-color: white">
        <div class="">

            <div class="col-md-12 col-sm-12 col-xs-12">

                <div class="panel panel-info">
                    <div class="panel-heading">筛选</div>
                    <div class="panel-body">
                        <form action=" " method="get" class="form-horizontal" role="form" id="searchform">

                            <div class="col-xs-12 col-sm-3 col-md-3col-lg-3">
                                <div class="input-group">
                                    <span class="input-group-addon">订单号</span>
                                    <input class="form-control" name="out_trade_no">
                                </div>
                            </div>
                            <div class="col-xs-12 col-sm-3 col-md-3col-lg-3">
                                <div class="input-group">
                                    <span class="input-group-addon">用户id</span>
                                    <input class="form-control" name="openid">
                                </div>
                            </div>
                            <div class="col-xs-12 col-sm-3 col-md-3col-lg-3">
                                <div class="input-group">
                                    <span class="input-group-addon">手机号</span>
                                    <input class="form-control" name="mobile">
                                </div>
                            </div>
                            <div class="col-xs-12 col-sm-3 col-md-3col-lg-3">
                                <div class="input-group">
                                    <span class="input-group-addon">状态</span>
                                    <select  class="form-control js-select2" name="use">
                                        <option value="">请选择状态</option>
                                        <option value="-1">未使用</option>
                                        <option value="1">已使用</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-xs-12 col-sm-4 col-md-3 col-lg-3">
                                <button class="btn btn-default" type="button" id="btnseach"><i class="fa fa-search"></i> 搜索</button>
                            </div>

                        </form>
                    </div>
                </div>
            </div>

            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>   {$xtitleb}  > {$xtitlea}列表</h2>

                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">

                        <div class="ibox-content" id="pfrom">
                            <!--data-detail-view="true"   data-detail-formatter="detailFormatter"-->
                            <table id="table" class="dotable" data-toggle="table"
                                   data-url="{php echo $this->createWebUrl($do, array('op'=>'group_getseachjson'));}"
                                   data-sort-name="id" data-sort-order="desc" data-query-params="queryparams"
                                   data-page-number="1" data-page-size="15" data-mobile-responsive="true">
                                <thead>
                                <tr>
                                    <th data-checkbox="true"></th>
                                    <th data-field="" data-sortable="true" data-formatter="service">项目信息</th>
                                    <th data-field="" data-sortable="true" data-formatter="order">订单信息</th>
                                    <th data-field="" data-sortable="true" data-formatter="user">用户信息</th>
                                    <th data-field="status" data-sortable="true" data-formatter="status">状态</th>
                                    <th data-events="operateEvents" data-formatter="operateFormatter" data-width="10%"
                                        data-title="操作" data-align="center">操作
                                    </th>
                                </tr>
                                </thead>
                            </table>
                            <a class="btn btn-primary" data-toggle="modal" data-target="#selectUrl">导出Excel</a>
                            <br>
                            <!--<br>-->
                            <!--说明：-->
                            <!--<a class="btn btn btn-default btn-xs"> <i class="fa fa-square-o fa-lg"></i></a>禁用 <a-->
                            <!--class="btn btn-primary  btn-xs"> <i class="fa fa-check-square-o fa-lg"></i></a> 开启-->
                        </div>

                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<!--底部链接选择-->
<div class="modal fade" id="selectUrl">
    <div class="modal-dialog">
        <style>
            #selectUrl .modal-body {padding: 10px 15px;  }
            #selectUrl .tab-pane {  margin-top: 5px;  min-height: 400px;  max-height: 400px;  overflow-y: auto;  }
            #selectUrlTab {  margin-bottom: 10px;  }
            #selectUrl .modal-body nav {  margin-bottom: 5px; }
        </style>
        <div class="modal-content">
            <div class="modal-header">
                <button data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">选择导出字段</h4>
            </div>
            <div class="modal-body">
                <ul class="nav nav-tabs" id="selectUrlTab">
                    <li role="presentation" class="active"><a href="#sut_shop">字段</a></li>
                </ul>
                <div class="tab-content ">
                    <div class="tab-pane active" id="sut_shop">
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="store_name" title="校区">校区</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="title" title="服务项目">服务项目</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="total" title="人数">人数</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="out_trade_no" title="订单号">订单号</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="wx_out_trade_no" title="微信订单号">微信订单号</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="amount" title="应付款">应付款</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="o_amount" title="实付款">实付款</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="openid" title="用户id">用户id</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="name" title="姓名">姓名</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="mobile" title="手机号">手机号</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="sign" title="自定义字段">自定义字段</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="content" title="备注">备注</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="status_name" title="状态">状态</nav>
                        <nav class="btn btn-default btn-sm" data-status="-1" data-name="createtime" title="添加时间">添加时间</nav>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" id="select-btn" type="button">导出</button>
                <button data-dismiss="modal" class="btn btn-default" id="select-close" type="button">关闭</button>
            </div>
            <script>
                $(function(){
                    $("#selectUrl .modal-body nav").click(function(){
                        var status=$(this).attr("data-status");
                        if(status==1){
                            $(this).removeClass("btn-primary").addClass("btn-default");
                        }else if(status==-1){
                            $(this).removeClass("btn-default").addClass("btn-primary");
                        }
                        $(this).attr("data-status",-status);
                    });
                    $("#select-btn").click(function(){
                        var data=[];
                        $("#selectUrl .modal-body").find("nav").each(function(){
                            var status=$(this).attr("data-status");
                            var name=$(this).attr("data-name");
                            var title=$(this).attr("title");
                            var data2={name:title,key:name,status:status};
                            data.push(data2);
                        });
                        var url="{url 'site/entry/'.$_GPC['do'],array('m'=>$_GPC['m'],'op'=>'group_export','version_id'=>$_GPC['version_id'])}";
                        var param=$("#searchform").serialize();
                        url=url+'&xc='+JSON.stringify(data)+'&'+param;
                        location.href=url;
                        $("#select-close").click();
                    });
                })
            </script>
        </div>
    </div>
</div>
<!--底部链接选择 end-->
{template 'common/listfoot'}
<script type="text/javascript">
    $(".ajax-select").select2({
        language:'zh-CN',
        ajax: {
            url: "{php echo $this->createWebUrl($do, array('op'=>'getseachselect'));}",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            processResults: function (data, params) {

//                console.log(data);
//             console.log("AAAAAAAAAAAAAAAA");
//                return {
//                    results: data
//                };



                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                params.page = params.page || 1;
                return {
                    results: data.rows,
                    pagination: {
                        more: (params.page * 30) < data.total
                    }
                };

//
//                params.page = params.page || 1;
//                return {
//                    results: data.rows,
//                    pagination: {
//                        more: (params.page * 30) < data.total
//                    }
//                };
            },
            cache: true
        },
        placeholder: '请选择分类',
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 0,
        templateResult: formatRepo,
        templateSelection: formatRepoSelection
    });

    function formatRepo (repo) {

        if (repo.loading) {
            return repo.text;
        }
        return repo.text;
//这里可以自定义返回格式
//        console.log(repo);
//        var markup = "<div class='select2-result-repository clearfix'>" +
//            "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
//            "<div class='select2-result-repository__meta'>" +
//            "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
//
//        if (repo.description) {
//            markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
//        }
//
//        markup += "<div class='select2-result-repository__statistics'>" +
//            "<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
//            "<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
//            "<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
//            "</div>" +
//            "</div></div>";
//
//        return markup;
    }
    function formatRepoSelection (repo) {
        return repo.text || repo.id;
    }
    var htojb={};
    $saechparas=null;
    $(function () {
        $("#btnseach").click(function () {
            setseach()
            $("#table").bootstrapTable('refresh');
        })
        $(".js-select2").select2({ theme: "bootstrap"});
        var htpid={};
        $('[name="pid"]').find("option").each(function () {
            htpid[$(this).attr("value")]=$(this).text();
        })

        htojb["pid"]=htpid;
    })
    function setseach() {
        $saechparas = {};
        $saechparas = $("#searchform").serializeArray();

    }
    function queryparams(params) {

        if ($saechparas == null) {
            setseach()
        }

        $.each($saechparas, function (i, field) {
            if(field.name.indexOf("[]")>0){

                var kname=field.name.replace("[]","");
                if( typeof(params[kname]) ==="undefined"){
                    params[kname]=[];
                }
                params[kname].push(field.value)
            }
            else
            {
                params[field.name] = field.value;
            }

        });

        return params;
    }
    $(function () {
        $("#delselct").click(function () {

            $pdadta = $("#table").bootstrapTable('getSelections');
            if ($pdadta.length < 1) {
                swal({
                    title: "错误",
                    text: "选选择删除内容",
                    type: "error",
                    timer: 2000
                });
            }
            else {
                swal({
                    title: '确认删除吗',
                    text: "确认删除吗" + $pdadta.length + "记录",
                    showCancelButton: true,
                    closeOnConfirm: false,
                    showLoaderOnConfirm: true,
                    confirmButtonText: "确认",
                    cancelButtonText: "取消",
                    preConfirm: function () {
                        return new Promise(function (resolve) {
                            var _postdata = {};
                            var ids = [];
                            $.each($pdadta, function (i, field) {
                                ids.push(field["id"]);
                            });
                            _postdata["ids"] = ids.join(",");
                            $.ajax("{php echo $this->createWebUrl($_GPC['do'], array('op'=>'new_delete'));}", {
                                method: 'POST',
                                data: _postdata,
                                dataType: 'json'
                            }).done(function (response) {
                                if (parseInt(response["status"]) === 1) {
                                    xc_edit_message({
                                        title: "提示",
                                        text: "删除成功",
                                        type: "success",
                                        timer: 1500
                                    });

                                    $("#table").bootstrapTable('refresh');
                                }
                                else {
                                    xc_edit_message({
                                        title: "错误",
                                        text: "删除失败",
                                        type: "error",
                                        timer: 2000
                                    });
                                }
                            });


                        });
                    },
                    allowOutsideClick: false
                })

            }

        })

    })
    $saechparas = null;
    function setseach() {
        $saechparas = {};
        $saechparas = $("#searchform").serializeArray();
    }
    $.extend($.fn.bootstrapTable.defaults, {
        method: 'post',
        pagination: true,
        sidePagination: 'server',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
//        showRefresh: true,
        onClickCell: function (field, value, row, $element) {


        },
        onLoadSuccess: function () {
//            $("#pfrom").find(".per").each(function () {
//                $(this).qrcode({
//                    text: $(this).attr("href"),
//                    height: 150,
//                    width: 150
//                });
//            })
        }
    });
    $.extend($.fn.bootstrapTable.columnDefaults, {
        align: 'center',
        valign: 'middle'
    });


    function operateFormatter(value, row, index) {
        var myhtml = [];
        if(row['status']==1 && row['group_status']==1 && row['use']==-1){
            myhtml.push(
                    '<a class="btn btn-primary btn-xs edit">',
                    '使用',
                    '</a>'
            );
        }else if(row['status']==2 && row['tui_status']==-1){
            myhtml.push(
                    '<a class="btn btn-primary btn-xs tui">',
                    '退款',
                    '</a>'
            );
        }
        return myhtml.join('');
    }

    function bimg(value, row, index) {
        var myhtml = [];
        var bimg=row['bimg'];
        console.log(bimg);
        myhtml.push(
                '<img src="'+bimg+'" width="150" height="60"/>'
        );
        return myhtml.join('');
    }



    var operateEvents = {
        "click a.edit": function (e, value, row, index) {
            $actindex = index;
            var _postdata = {};
            _postdata["id"] = row["id"];
            swal({
                title: '确认使用吗',
                showCancelButton: true,
                closeOnConfirm: false,
                showLoaderOnConfirm: true,
                confirmButtonText: "使用",
                cancelButtonText: "取消",
                preConfirm: function () {
                    return new Promise(function (resolve) {
                        $.ajax("{php echo $this->createWebUrl($_GPC['do'], array('op'=>'statuschange'));}", {
                            method: 'POST',
                            data: _postdata,
                            dataType: 'json'
                        }).done(function (response) {
                            if (parseInt(response["status"]) === 1) {
                                var is_use=row['is_use'];
                                var can_use=row['can_use'];
                                var use=-1;
                                is_use=parseInt(is_use)+1;
                                if(parseInt(is_use)==parseInt(can_use)){
                                    use=1;
                                }
                                $("#table").bootstrapTable('updateRow', {
                                    index: $actindex,
                                    row: {
                                        is_use: is_use,
                                        use:use
                                    }
                                });
                                swal('操作成功!', '操作成功!', 'success');
                            }
                            else {
                                swal('操作失败!', '操作失败!', 'error');

                            }
                        });

                        //  resolve();

                    });
                },
                allowOutsideClick: false
            })
        },
        "click a.tui": function (e, value, row, index) {
            $actindex = index;
            var _postdata = {};
            _postdata["id"] = row["id"];
            swal({
                title: '确认退款吗',
                showCancelButton: true,
                closeOnConfirm: false,
                showLoaderOnConfirm: true,
                confirmButtonText: "退款",
                cancelButtonText: "取消",
                preConfirm: function () {
                    return new Promise(function (resolve) {
                        $.ajax("{php echo $this->createWebUrl($_GPC['do'], array('op'=>'tui'));}", {
                            method: 'POST',
                            data: _postdata,
                            dataType: 'json'
                        }).done(function (response) {
                            if (parseInt(response["status"]) === 1) {
                                $("#table").bootstrapTable('updateRow', {
                                    index: $actindex,
                                    row: {
                                        tui_status:1
                                    }
                                });
                                swal('操作成功!', '操作成功!', 'success');
                            }
                            else {
                                swal('操作失败!', '操作失败!', 'error');

                            }
                        });

                        //  resolve();

                    });
                },
                allowOutsideClick: false
            })
        },
    };

    var operatesorts = {
        'change input.sorts': function (e, value, row, index) {

            var _postdata = {};
            _postdata["id"] = row["id"];
            _postdata["sorts"] = $(this).val();
            $actindex = index;

            $.ajax("{php echo $this->createWebUrl($_GPC['do'], array('op'=>'updatesorts'));}", {
                method: 'POST',
                data: _postdata,
                dataType: 'json'
            }).done(function (response) {
                if (parseInt(response["status"]) === 1) {
                    xc_list_message({
                        title: "提示",
                        text: "操作成功",
                        type: "success",
                        timer: 1500
                    });

                    $("#table").bootstrapTable('updateRow', {
                        index: $actindex,
                        row: {
                            sort:  _postdata["sorts"]
                        }
                    });
                }
                else {
                    xc_list_message({
                        title: "错误",
                        text: "操作失败",
                        type: "error",
                        timer: 2000
                    });
                }
            });


        }
    };
    var operatestatus = {
        'click a': function (e, value, row, index) {
            var _postdata = {};
            _postdata["id"] = row["id"];
            _postdata["status"] = $(this).attr("data-value");
            $actindex = index;

            $.ajax("{php echo $this->createWebUrl($_GPC['do'], array('op'=>'midstatus'));}", {
                method: 'POST',
                data: _postdata,
                dataType: 'json'
            }).done(function (response) {
                if (parseInt(response["status"]) === 1) {
                    xc_list_message({
                        title: "提示",
                        text: "操作成功",
                        type: "success",
                        timer: 1500
                    });

                    $("#table").bootstrapTable('updateRow', {
                        index: $actindex,
                        row: {
                            status: -_postdata["status"]
                        }
                    });
                }
                else {
                    xc_list_message({
                        title: "错误",
                        text: "操作失败",
                        type: "error",
                        timer: 2000
                    });
                }
            });
        }
    };
    //格式化状态
    function formatstatus(value, row, index) {
        if (value == -1) {

            return '<a class="btn btn btn-default btn-xs" data-value="-1"  data-filed="status" > <i class="fa fa-square-o fa-lg"></i></a> ';
        }
        else {
            return '<a class="btn btn-primary  btn-xs"  data-value="1" data-filed="status" > <i class="fa fa-check-square-o fa-lg"></i></a>'
        }
    }
    function  formatpid(value, row, index) {
        if(typeof(htojb["pid"][value])==="undefined"){

            return "分类已删除";
        }
        else {
            return htojb["pid"][value];

        }
    }
    //格式化排序
    function formatsorts(value, row, index) {

        return ' <input class="sorts" value="'+value+'">';

    }

    function service(value, row, index){
        var myhtml=[];
        if(row['store_name']!="" && row['store_name']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">校区:'+row['store_name']+'</p>'
            );
        }
        myhtml.push(
                '<p style="text-align: left;margin: 0;">课程:'+row['title']+'</p>',
                '<p style="text-align: left;margin: 0;">人数:'+row['total']+'</p>'
        );
        return myhtml.join("");
    }

    function order(value, row, index){
        var myhtml=[];
        myhtml.push(
                '<p style="text-align: left;margin: 0;">订单号:'+row['out_trade_no']+'</p>'
        );
        if(row['o_amount']!="" && row['o_amount']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">应付款:'+row['amount']+'元</p>'
            );
        }else{
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">应付款:免费</p>'
            );
        }
        if(row['o_amount']!="" && row['o_amount']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">实付款:'+row['o_amount']+'元</p>'
            );
        }
        if(row['coupon_price']!="" && row['coupon_price']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">优惠:'+row['coupon_price']+'元</p>'
            );
        }
        if(row['share_one_openid']!="" && row['share_one_openid']!=null && row['share_one_fee']!="" && row['share_one_fee']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">一级分销用户:'+row['share_one_openid']+'</p>'
            );
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">一级分销佣金:'+row['share_one_fee']+'元</p>'
            );
        }
        if(row['share_two_openid']!="" && row['share_two_openid']!=null && row['share_two_fee']!="" && row['share_two_fee']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">二级分销用户:'+row['share_two_openid']+'</p>'
            );
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">二级分销佣金:'+row['share_two_fee']+'元</p>'
            );
        }
        if(row['share_three_openid']!="" && row['share_three_openid']!=null && row['share_three_fee']!="" && row['share_three_fee']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">三级分销用户:'+row['share_three_openid']+'</p>'
            );
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">三级分销佣金:'+row['share_three_fee']+'元</p>'
            );
        }
        myhtml.push(
                '<p style="text-align: left;margin: 0;">下单时间:'+row['createtime']+'</p>'
        );
        return myhtml.join("");
    }

    function user(value, row, index){
        var myhtml=[];
        myhtml.push(
                '<p style="text-align: left;margin: 0;">用户id:'+row['openid']+'</p>',
                '<p style="text-align: left;margin: 0;">姓名:'+row['name']+'</p>',
                '<p style="text-align: left;margin: 0;">手机号:'+row['mobile']+'</p>'
        );
        if(row['content']!="" && row['content']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">备注:'+row['content']+'</p>'
            );
        }
        if(row['tui']!="" && row['tui']!=null){
            myhtml.push(
                    '<p style="text-align: left;margin: 0;">推荐人:'+row['tui']+'</p>'
            );
        }
        if(row['sign']!="" && row['sign']!=null){
            for(var i=0;i<row['sign'].length;i++){
                myhtml.push(
                        '<p style="text-align: left;margin: 0;">'+row['sign'][i]['name']+':'+row['sign'][i]['value']+'</p>'
                );
            }
        }
        return myhtml.join("");
    }
    function status(value, row, index){
        var myhtml=[];
        if(row['status']==1){
            if(row['group_status']==-1){
                myhtml.push('拼团中');
            }else if(row['group_status']==1){
                if(row['use']==1){
                    myhtml.push('已使用');
                }else{
                    myhtml.push('未使用 '+row['is_use']+'/'+row['can_use']);
                }
            }
        }else if(row['status']==2){
            if(row['tui_status']==-1){
                myhtml.push('退款中');
            }else if(row['tui_status']==1){
                myhtml.push('已退款');
            }
        }
        return myhtml.join('');
    }
</script>
</body>
</html>